// pages/login/login.js
var api = require('../../api.js');
var util = require('../../utils/utils.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url:"/pages/index/index",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    app.getStoreData();
    if (options.url != undefined){
      var url = unescape(options.url);
      console.log("url:", options.url)
      var that = this;
      that.setData({
        url: url,
      });
    }
  },

  login: function() {
    var pages = getCurrentPages();
    var page = pages[(pages.length - 1)];
    var that = this;
    wx.showLoading({
      title: "正在登录",
      mask: true,
    });
    wx.login({
      success: function(res) {
        if (res.code) {
          var code = res.code;
          wx.getUserInfo({
            success: function(res) {
              //console.log(res);
              getApp().request({
                url: api.passport.login,
                method: "post",
                data: {
                  code: code,
                  user_info: res.rawData,
                  encrypted_data: res.encryptedData,
                  iv: res.iv,
                  signature: res.signature
                },
                success: function(res) {
                  if (res.code == 0) {
                    wx.setStorageSync("access_token", res.data.access_token);
                    wx.setStorageSync("user_info", {
                      nickname: res.data.nickname,
                      avatar_url: res.data.avatar_url,
                      is_distributor: res.data.is_distributor,
                      parent: res.data.parent,
                      id: res.data.id,
                      is_clerk: res.data.is_clerk
                    });
                    // console.log(res);
                    // var parent_id = wx.getStorageSync("parent_id");
                    var p = getCurrentPages();
                    var parent_id = 0;
                    if (p[0].options.user_id != undefined) {
                      var parent_id = p[0].options.user_id;
                    } else if (p[0].options.scene != undefined) {
                      var parent_id = p[0].options.scene;
                    }
                    // console.log(parent_id, p[0].options.scene, p[0].options.user_id);
                    getApp().bindParent({
                      parent_id: parent_id || 0
                    });

                    if (page == undefined) {
                      return;

                    }
                    var loginNoRefreshPage = getApp().loginNoRefreshPage;
                    for (var i in loginNoRefreshPage) {
                      if (loginNoRefreshPage[i] === page.route)
                        return;
                    }
                    wx.redirectTo({
                      // url: "/" + page.route + "?" + util.objectToUrlParams(page.options),
                      url: that.data.url,
                      fail: function() {
                        wx.switchTab({
                          url: "/" + page.route,
                        });
                      },
                    });
                  } else {
                    wx.showToast({
                      title: res.msg
                    });
                  }
                }
              });
            },
            fail: function(res) {
              that.login();
            }
          });
        } else {
          console.log(res);
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log(111);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})